/**
 * 
 */
/**
 * @author shanghaitao
 *
 */
package com.starbaba.system.usergroup.action;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonObject;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.common.ResultJson;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.system.usergroup.model.UsergroupVO;
import com.starbaba.system.usergroup.model.UserGroupModel;
import com.starbaba.system.usergroup.service.UserGroupService;


@SuppressWarnings({"serial","rawtypes","unchecked","unused"})
public class UserGroupAction extends BaseAction implements ModelDriven {
	
    @Resource(name="UserGroupServiceImpl")
	private UserGroupService service;
	
	private UserGroupModel model;
	
	/**
	 * 
	 * @Title: UserGroupList
	 * @Description: 【用户列表】
	 * @param
	 * @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public void userGroupList(){
		List groupid = null;
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(
				Struts2Utils.getRequest(), paramsMap);
		//type为1--查询全部(备用)
		String type = (String) paramsMap.get("type");
		String thisUserGroupId = (String) paramsMap.get("thisUserGroupId");
		//suittype为1,查询时需要groupid
		String suittype = (String) paramsMap.get("suittype");
		
		if(thisUserGroupId != null){
			String[] menuList2= thisUserGroupId.split("#");
			groupid = Arrays.asList(menuList2);
		}
		parsePageRequest();
		try {
			PageResponse pageResponse = new PageResponse();
			Integer startPoint = pageRequest.getIDisplayStart();
			Integer displayLength = pageRequest.getIDisplayLength();
			if(null == type){
					startPoint = 0;
					displayLength = 100000;
			}
			List list = service.getDataList(startPoint , displayLength,model,pageResponse,getService(),groupid,suittype);
			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());
			String jsonStr = JsonUtils.toJson(pageResponse, null, true, null, false);
			sendJsonMessage(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @Title: addUser
	 * @Description: 【新增用户】
	 * @param
	 * @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public void addGroup() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(
				Struts2Utils.getRequest(), paramsMap);
		String groupname = (String) paramsMap.get("groupname");
		String creator = (String) paramsMap.get("creator");
		java.util.Date currentTime = new Date();
		try {
			//插入到usergroup表
			UsergroupVO entity = new UsergroupVO();
			entity.setGroupname(groupname);
			entity.setMemo(groupname);
			entity.setCreator(creator);
			entity.setLasteditor(creator);
			entity.setCreatetime(currentTime);
			entity.setLasttime(currentTime);
			//新增组时默认设置为有效
			entity.setStatus("1");
			
			List list = this.getService().doFindByProperty(UsergroupVO.class, "groupname", groupname);
			//如果已经有相同的userid,不允许增加
			if(list.size() > 0){
				sendErrMsg("已有同名组");
			}else{
				Object ret = this.getService().doCreate(UsergroupVO.class, entity);
				JsonObject result = new JsonObject();
				ResultJson resultJs = new ResultJson();
				resultJs.msg = "新增成功";
				result.add("result", resultJs.toJSON());
				sendJsonMessage(result.toString());
			}	
		} catch (Exception e) {
			sendErrMsg(e.getMessage());
		}

	}
	
	
	private void sendErrMsg(String msg) {
		JsonObject result = new JsonObject();
		ResultJson resultJs = new ResultJson();
		resultJs.msg = msg;
		result.add("result", resultJs.toJSON());
		try {
			sendJsonMessage(result.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	@Override
	public Object getModel() {
		if(model == null)
		{
			model = new UserGroupModel();
		}
		return model;
	}
}